/**
 * 
 * @auth Chunning
 * @purpose manage focus & tab index...
 * @date 2009-2-4
 * 
 * */
package utils.focus
{
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Stage;
	import flash.events.FocusEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldType;
		
	
	
	public class FocusManager
	{
		//START...
		
		//PROPERTY...
		
		private static var nowFocus:FocusManager;
		
		private var target:DisplayObjectContainer;	
		
		private var inputTxts:Array;	
		
		//constructor...
		public function FocusManager(target:DisplayObjectContainer)
		{
			
			this.target = target;
			
			this.target.addEventListener(FocusEvent.FOCUS_IN,this.focusInHandler);
						
			this.deactive();
									
		}
		
		//methods...
		
		//focus in handler....
		private function focusInHandler(evt:FocusEvent):void
		{
									
			this.active();
						
		}
			
				
		//record now focus...
		public function active():void
		{
			
			if(nowFocus === this)
			{
				
				return;
				
			}
					
			if(nowFocus is FocusManager)
			{
				
				nowFocus.deactive();
				
			}
			
			nowFocus = this;
			
			target.tabChildren = true;	
			
			this.setTabIndex();					
		}
		
		//deactive...
		public function deactive():void
		{
			
			this.target.tabChildren = false;
			
		}
		
		//set tabindex...
		public function setTabIndex():void
		{
			
			inputTxts = new Array;
			
			this.getTextField(this.target);
			
			inputTxts.sort(this.sortHandler,Array.NUMERIC);
			
			inputTxts.forEach(setItemTabIndex);
						
		}	
		
		//get textfield....
		private function getTextField(container:DisplayObjectContainer):void
		{
			
			for(var i:int = 0;i < container.numChildren;i++)
			{
								
				if(container.getChildAt(i) is TextField && (container.getChildAt(i) as TextField).type == TextFieldType.INPUT)
				{
				
					inputTxts.push(container.getChildAt(i));
					
				}
				
				if(container.getChildAt(i) is DisplayObjectContainer)
				{
					
					this.getTextField(container.getChildAt(i) as DisplayObjectContainer);
					
				}
				
			}
			
			
		}
		
		//sort handler...
		private function sortHandler(a:TextField,b:TextField):int
		{
			
			var pointA:Point = a.localToGlobal(new Point(0,0));
			
			var pointB:Point = b.localToGlobal(new Point(0,0));
			
			var reValue:int;
			
			reValue = pointA.y < pointB.y ? -1 : 1;
			
			if(pointA.y == pointB.y)
			{
				
				reValue = pointA.x < pointB.x ? -1 : 1;
				
			}
			
			return reValue;
			
		}
		
		//set tab index...
		private  function setItemTabIndex(item:*,index:int,arr:Array):void
		{
				
			item.tabIndex = index;
											
		}
		
		//set focus...
		public function setFocus(interactive:InteractiveObject):void
		{
			
			if(this.target.stage is Stage)
			{
				
				this.target.stage.focus = interactive;
				
			}
			
		}	
		
		//end
	}
}